<?php
// +----------------------------------------------------------------------
// | 为分销商配置转分销产品  
// +----------------------------------------------------------------------
// | TongBaoChun <501468940@qq.com>
// +----------------------------------------------------------------------
// | ----/--/--
// +----------------------------------------------------------------------
include '/var/www/html/new/com.inc.php';

if ($_REQUEST['auth'] == 'admin') {
	$_SESSION = $_REQUEST['session'];
}
if(!isset($_SESSION['sid'])) exit('{"status":"fail","msg":"登陆超时","code":0}');




$sid = $_SESSION['sid'];   // 会员 ID
$did = $_REQUEST['did']+0; // 分销商 ID

if(isset($_REQUEST['act']) && $_REQUEST['act']=='turnoffEvo') goto EEEE;




$set_pris = $_REQUEST["set_pris"]; // 配置价格

foreach ($set_pris as $price) {
	if ($price >= 10000) {
		exit("<script>alert('价格配置必须限制在10000以内!')</script>");
	}
}

$src_pris = $_REQUEST["src_pris"]; // 会员分销价
$pids_arr = array_unique(array_values($_REQUEST['src_pids']));// 页面所有产品
$pids_str = implode(',',$pids_arr);
$pids_str = rtrim($pids_str,',');
// 1.先删除页面上显示的产品的所有记录
$size = count($pids_arr);
$sql="select id,pid,aids from `pft_p_apply_evolute` where sid=$sid and fid=$did and pid in ($pids_str)";
$sql="delete from `pft_p_apply_evolute` where sid=$sid and fid=$did and pid in ($pids_str) limit $size";
$GLOBALS['le']->query($sql);

// 2.给设置的产品加上购买的权限
$setPri = array();
foreach($set_pris as $key=>$price){
	$tmp = explode('_',$key);
	$pid = $tmp[0];
	$aid = $tmp[1];
	if(!isset($set_pris[$key])) continue;// 没有设置价格
	if(isset($setPri[$pid])) continue;// 已经为该分销商设置了一个转分销链
	$sql="select id,status,aids,lvl,sourceid from `pft_p_apply_evolute` where fid=$sid and sid=$aid and pid=$pid limit 1";

	$GLOBALS['le']->query($sql);
	$GLOBALS['le']->fetch_assoc();
	$aids_n = $GLOBALS['le']->f('aids').','.$sid;
	$lvl_n = $GLOBALS['le']->f('lvl') + 1;
	$sourceid = $GLOBALS['le']->f('sourceid');
	$ins = "insert into `pft_p_apply_evolute` set aids='$aids_n',sourceid=$sourceid,lvl=$lvl_n,status=0,sid=$sid,fid=$did,pid=$pid,rectime=now()";
	if($GLOBALS['le']->query($ins)){// 新增权限
		$setPri[$pid] = $aid;// 多个供应商只允许转分销一个
	};
}
// 3.配置价格
if(count($setPri)){
	
	$exis = $exis_u = $exis_uid = array();
	$pids = array_keys($setPri);
	$sql="select pid,id,tid from `uu_priceset` where pid=$did and aid=$sid and tid in (".implode(',',$pids).")";
	$GLOBALS['le']->query($sql);
	while($row=$GLOBALS['le']->fetch_assoc()){
		$exis[$row['tid']] = $row['tid'];// 获取已配置价格的产品 id 
		$exis_uid[$row['tid']]        = $row['id'];
	}

	foreach($setPri as $pid=>$aid){// $aid 为账号的上级供应商
		// echo $pid;
		$pk = $pid.'_'.$aid;
		$new_price = $set_pris[$pk]; // 新设置的价格
		if(!is_numeric($new_price)) continue;
		$sup_price = $src_pris[$pk]; // 会员分销商
		$diff = $new_price - $sup_price;
		if($diff<0) continue;// 给分销商的价格比自己还低，X
		$diff = $diff*100;
		if(isset($exis[$pid])){
			$uid = $exis_uid[$pid];
			$sql="update `uu_priceset` set dprice=$diff where id=$uid limit 1";			
			
		}else{// 给分销商设置价格

			$sql="insert into `uu_priceset` set dprice=$diff,tid=$pid,pid=$did,aid=$sid";
		}

		$GLOBALS['le']->query($sql);
	}
}

if ($_REQUEST['auth'] == 'admin') {
	exit(json_encode(array('status' => 1, '配置成功')));
}

echo "<script>alert('配置成功!');parent.window.location.href='".$_SERVER['HTTP_REFERER']."'</script>";
exit;












EEEE:// 去除购买权限
$pid = $_REQUEST['pid'] + 0;
$sql = "select id,status,aids from `pft_p_apply_evolute` where fid=$did and sid=$sid and pid=$pid limit 1";
$GLOBALS['le']->query($sql);
if(!$GLOBALS['le']->fetch_assoc()) exit('{"status":"fail","msg":"数据出错"}');
if($GLOBALS['le']->f('status')==1) exit('{"status":"fail","msg":"权限已断开"}');

$eid = $GLOBALS['le']->f('id');
$sql = "update `pft_p_apply_evolute` set status=1,active=0 where id=$eid and fid=$did and sid=$sid and pid=$pid limit 1";
if(!$GLOBALS['le']->query($sql)) exit('{"status":"fail","msg":"权限断开出错"}');

// 去除底下的所有购买权限
$aids = $GLOBALS['le']->f('aids').','.$did;
$evolist = RecvEvoluteRow($pid, $did, $aids);
if(count($evolist)) foreach($evolist as $row) $idBox_a[] = $row['id'];
$idBox_l = count($idBox_a);
if($idBox_l==0) exit('{"status":"success","msg":"设置成功"}');
$idBox_s = implode(',', $idBox_a);
$sql = "update pft_p_apply_evolute set status=1,active=0 where id in ($idBox_s) and pid=$pid limit $idBox_l";
$GLOBALS['le']->query($sql);
exit('{"status":"success","msg":"设置成功"}');


//  递归获取树形产品供应链
function RecvEvoluteRow($pid, $sid, $aids=''){
	
	$container = array();
	$sql = "select * from pft_p_apply_evolute where pid=$pid and sid=$sid and status=0";
	if($aids!='') $sql = $sql." and aids='$aids'";
	$GLOBALS['le']->query($sql);
	while($row=$GLOBALS['le']->fetch_assoc()) $container[] = $row;
	if(count($container)){
		foreach($container as $row){
			$container = array_merge($container, RecvEvoluteRow($pid, $row['fid'], $row['aids'].','.$row['fid']));
		}
	}
	return $container;
}